<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\JsonResponse;


class RequireAdmin {
  public function __construct(Guard $auth)
  {
    $this->auth = $auth;
  }
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
    if ($this->auth->guest() || $this->auth->user() && $this->auth->user()->type == 1)
    {
      if ($request->ajax())
      {
        return new JsonResponse([
          'error_code' => 302,
          'error_message' => '请登陆',
          'url' => $request->getBaseUrl().'/login'
        ], 200);
      }
      else
      {
        $url = $request->getBaseUrl().'/login';
        return redirect($url);
      }
    }
        return $next($request);
    }
}
